
import UIKit

class TF_GoodsListViewCell: UITableViewCell {

    @objc public var cellWidth:CGFloat = 0
    @objc public var cellHeight:CGFloat = 0
    @objc public let picture = UIImageView()
    //二维码
    @objc public let qrcode = UIImageView()
    //下划线
    @objc public let Underline = UIView()
    @objc public let nameLabel = UITextView()
    //没有维码时的名字
    @objc public let longNameLabel = UITextView()
    @objc public let link_textLabel = UILabel()
    //商 品类型
    @objc public let tab = UILabel()
    @objc public let label99 = UILabel()
    @objc public let label888 = UILabel()
    @objc public let qrcodeLabel = UILabel()

    @objc public var model:TFgoods?{
        didSet {
            if ((model) != nil)
            {
                let pictureUrl:URL? = URL.init(string: model!.img as String)
                self.picture.sd_setImage(with: pictureUrl, completed: nil)
                
                
                let qrcodeUrl:URL? = URL.init(string: model!.qrcode as String)
                self.qrcode.sd_setImage(with: qrcodeUrl, completed: nil)
                
                let qrcodeStr:NSString = model!.qrcode as NSString
                //有二维码
                if (qrcodeStr.length > 0 ) {
                    self.nameLabel.text =  model!.name ;
                    self.longNameLabel.text = "";

                }else{
                    self.nameLabel.text = "";
                    self.longNameLabel.text = model!.name;

                }

                
                let nameLabelCX:CGFloat = 85 + 15 + 12.5;
                let nameLabelY:CGFloat = 13;
                
               
                let nameLabelWidth:CGFloat = (self.cellWidth - 85 - 15)  - nameLabelCX - 12.5;
                let nameLabelHight:CGFloat = self.cellHeight - 58.5 - nameLabelY;
                //有二维码时的名字   短
                self.nameLabel.frame = CGRect(x: nameLabelCX, y: nameLabelY, width: nameLabelWidth, height: nameLabelHight)
                
                //没胡二维码时的名字  长
                self.longNameLabel.frame = CGRect(x: nameLabelCX, y: nameLabelY, width: self.cellWidth  - nameLabelCX - 12.5, height: nameLabelHight)
                
        
                let attr: [NSAttributedString.Key : Any] = [.font: UIFont(name: "PingFang SC", size: 14) as Any,.foregroundColor: UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0,alpha:1), ]

                
                let link_text:NSString = model!.link_text as NSString
                let attrsSize = link_text.size(withAttributes: attr)
            
                var link_textLabelWidth:CGFloat = attrsSize.width + 11.5 + 11.5;
                
                //已经有二维码了, 不会再显示这个
            
                if (qrcodeStr.length > 0 || link_text.length == 0 ) {
                    link_textLabelWidth = 0;
                }
                let link_textLabelX:CGFloat = self.cellWidth - link_textLabelWidth - 15;
               
                let link_textLabelHeight:CGFloat = 30
                self.link_textLabel.layer.cornerRadius = link_textLabelHeight / 2
                
                let link_textLabelY:CGFloat = self.cellHeight - link_textLabelHeight - 15;
                self.link_textLabel.frame = CGRect(x: link_textLabelX, y: link_textLabelY, width: link_textLabelWidth, height: link_textLabelHeight)
                
             
                
                let attrString = NSMutableAttributedString(string: link_text as String)

                attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
                
                self.link_textLabel.attributedText = attrString;
                self.tab.isHidden = false
              
                //特价 限时  新品 钜惠 秒杀
                if model!.tab == "1"  {
                    self.tab.text = "特" + "价"
                }else if (model!.tab == "2") {
                    self.tab.text = "限时";
                }else if  (model!.tab == "3") {
                    self.tab.text = "新品"
                }else if  (model!.tab == "4") {
                    self.tab.text = "钜" + "惠"
                   
                }else if (model!.tab == "5") {
                    self.tab.text = "秒" + "杀"
                }else {
                    self.tab.text = "";
                    self.tab.isHidden = true
                }

                self.tab.frame = CGRect(x: 85 + 15 + 9, y: 62.5, width: 34, height: 18)
                
                
                let label99Hight:CGFloat = 22.5
                
                let label99Attrs: [NSAttributedString.Key : Any] = [.font: UIFont(name: "PingFang SC", size: 16) as Any,.foregroundColor: UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0,alpha:1), ]

                let now_PriceStr:NSString = "￥" + model!.now_Price as NSString
                let label99AttrsSize = now_PriceStr.size(withAttributes: label99Attrs)
                
            
                self.label99.frame = CGRect(x: self.tab.frame.origin.x, y: self.tab.frame.maxY + 4.5, width: label99AttrsSize.width, height: label99Hight)
                
                self.label99.text = ""
                
                if (now_PriceStr.length > 1 ) {
                    let aa:NSString = "￥"
                    let bb:NSString = model!.now_Price as NSString
                    self.label99.text = (aa as String) + (bb as String)
                    
                }
                
                let label888Attrs: [NSAttributedString.Key : Any] = [.font: UIFont(name: "PingFang SC", size: 14) as Any,.foregroundColor: UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0,alpha:1), ]
                
                var originalPriceStr:NSString = model!.originalPrice as NSString
                if originalPriceStr.length > 0 {
                    originalPriceStr = "￥" + model!.originalPrice as NSString
                }
                
                let label888AttrsSize = originalPriceStr.size(withAttributes: label888Attrs)
                

                self.label888.frame = CGRect(x: self.label99.frame.maxX + 2.5, y: self.label99.frame.origin.y, width: label888AttrsSize.width, height: label99Hight)

            
                let originalPriceStr222 = NSMutableAttributedString(string: originalPriceStr as String)

                originalPriceStr222.addAttributes(label888Attrs, range: NSRange(location: 0, length: attrString.length))
                
                self.label888.attributedText = originalPriceStr222;
                
                
                
                if (qrcodeStr.length > 0 ) {
                    self.qrcodeLabel.isHidden = false
                }else{
                    self.qrcodeLabel.isHidden = true
                }
            }
           
        }
        
    }
    
    @objc func qrcodeTouchUpInside()
    {
        let url:NSString = self.model!.url as NSString
        if (url.length > 0 ) {
            
            TalkfunSharedTools.openUrl(str: url as NSString )
          
        }
    }

    @objc func longPress(sender:UILongPressGestureRecognizer)
    {
        if ( sender.state == .began )
        {
            self.becomeFirstResponder()
            TalkfunSharedTools.openUrl(image: self.qrcode.image!)

        }
    
    }
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
      

        self.picture.backgroundColor = UIColor.clear
        self.picture.layer.cornerRadius = 4;
        self.contentView.addSubview(self.picture)
        
        
       
        self.qrcode.backgroundColor = UIColor.clear
        self.qrcode.layer.cornerRadius = 4;
        self.contentView.addSubview(self.qrcode)
        self.qrcode.isUserInteractionEnabled = true
        //长按
        let longPress = UILongPressGestureRecognizer.init(target: self, action:  #selector(longPress(sender:)))
        longPress.delegate = self;
        self.qrcode.addGestureRecognizer(longPress)
        self.qrcode.isUserInteractionEnabled = true
        
        //点按
        let qrcodeTapGestureRecognizer = UITapGestureRecognizer.init(target: self, action:  #selector(qrcodeTouchUpInside))

        self.qrcode.addGestureRecognizer(qrcodeTapGestureRecognizer)
    
        self.qrcodeLabel.numberOfLines = 0;
        self.qrcodeLabel.isHidden = true
        self.contentView.addSubview(self.qrcodeLabel)
        self.qrcodeLabel.font = UIFont.PingFang(size: 16)
        
        self.qrcodeLabel.textColor = UIColor(red: 255/255.0, green: 94/255.0, blue: 97/255.0, alpha: 1.0)
    
        self.qrcodeLabel.text = "长按购买";
        self.qrcodeLabel.textAlignment = .center;
        
        self.nameLabel.backgroundColor = UIColor.clear
        self.contentView.addSubview(self.nameLabel)
        self.nameLabel.font = UIFont.PingFang(size: 16)
        self.nameLabel.textColor = UIColor(red: 77/255.0, green: 83/255.0, blue: 88/255.0, alpha: 1.0)
        
        self.nameLabel.textContainer.lineFragmentPadding = 0.0;
        self.nameLabel.textContainerInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)

        self.nameLabel.isUserInteractionEnabled = false
        

        self.longNameLabel.backgroundColor = UIColor.clear
        self.longNameLabel.isUserInteractionEnabled = false
        self.contentView.addSubview(self.longNameLabel)
        self.longNameLabel.font = self.nameLabel.font;
        self.longNameLabel.textColor = UIColor(red: 77/255.0, green: 83/255.0 , blue: 88/255.0, alpha: 1.0)

        self.longNameLabel.textContainer.lineFragmentPadding = 0.0;
        self.longNameLabel.textContainerInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        
        self.Underline.backgroundColor = UIColor(red: 238/255.0, green: 238/255.0 , blue: 239/255.0, alpha: 1.0)
    
        self.contentView.addSubview(self.Underline)
        
        
        self.link_textLabel.numberOfLines = 0;
        self.link_textLabel.clipsToBounds = true
        self.contentView.addSubview(self.link_textLabel)
        self.link_textLabel.textAlignment = .center;
        self.link_textLabel.backgroundColor = UIColor(red: 255/255.0, green: 94/255.0, blue: 97/255.0, alpha: 1.0)
        
        
        
        self.link_textLabel.isUserInteractionEnabled = true
        //去看看
        let labelTapGestureRecognizer = UITapGestureRecognizer.init(target: self, action:  #selector(qrcodeTouchUpInside))
                  
        self.link_textLabel .addGestureRecognizer(labelTapGestureRecognizer)
        
   
        self.tab.textAlignment = .center
        self.addSubview(self.tab)
        self.tab.layer.cornerRadius = 1;
        self.tab.layer.borderColor = UIColor(red: 55/255.0, green: 129/255.0, blue: 129/255.0, alpha: 1.0).cgColor
      
        self.tab.layer.borderWidth = 0.5;
        self.tab.textColor = UIColor(red: 255/255.0, green: 159/255.0, blue: 129/255.0, alpha: 1.0)
        
        self.tab.font = UIFont.PingFang(size: 12)
        self.tab.isHidden = true
        
        //现在的
        self.label99.numberOfLines = 0;
        self.addSubview(self.label99)
        self.label99.font = UIFont.PingFang(size: 12)
        self.label99.textColor = UIColor(red: 255/255.0, green: 94/255.0, blue: 97/255.0, alpha: 1.0)
    
        self.label99.textAlignment = .center;
        
        self.label888.numberOfLines = 0;
        self.addSubview(self.label888)
        self.label888.font = UIFont.PingFang(size: 14)
        self.label888.textColor = UIColor(red: 187/255.0, green: 196/255.0, blue: 209/255.0, alpha: 1.0)
      
        self.label888.textAlignment = .center
       
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        self.picture.frame = CGRect(x: 15, y: 15, width: 85, height: 85)
    
        self.qrcode.frame = CGRect(x: self.frame.size.width - 85 - 15, y: 15, width: 85, height: 85)
        
        self.qrcodeLabel.frame = CGRect(x: self.qrcode.frame.origin.x, y: self.qrcode.frame.maxY + 10 , width: self.qrcode.frame.size.width, height: 22.5)

        self.Underline.frame = CGRect(x: 12, y: self.frame.size.height - 0.5, width: self.frame.size.width - 24, height: 0.5)
        
    }
}
